package com.party.core.service.member.impl;

import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.party.common.paging.DataParam;
import com.party.common.paging.Page;
import com.party.common.utils.BigDecimalUtils;
import com.party.common.utils.DateUtils;
import com.party.common.utils.LangUtils;
import com.party.core.dao.read.member.MemberMerchantCountReadDao;
import com.party.core.dao.read.member.MemberMerchantDetailReadDao;
import com.party.core.dao.write.member.MemberMerchantDetailWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.YesNoStatus;
import com.party.core.model.member.MemberMerchantCount;
import com.party.core.model.member.MemberMerchantDetail;
import com.party.core.model.member.MemberMerchantDetailQuery;
import com.party.core.model.member.MemberMerchantDetailWithMember;
import com.party.core.service.member.IMemberMerchantDetailService;
import com.sun.istack.NotNull;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 合作商统计详情服务实现
 * Created by wei.li.
 *
 * @date 2017/12/12 0012
 * @time 9:43
 */

@Service
public class MemberMerchantDetailService implements IMemberMerchantDetailService {

    @Autowired
    private MemberMerchantDetailReadDao memberMerchantDetailReadDao;

    @Autowired
    private MemberMerchantDetailWriteDao memberMerchantDetailWriteDao;

    @Autowired
    private MemberMerchantCountReadDao memberMerchantCountReadDao;

    /**
     * 新增
     * @param memberMerchantDetail 统计详情
     * @return 编号
     */
    @Override
    public String insert(MemberMerchantDetail memberMerchantDetail) {
        BaseModel.preInsert(memberMerchantDetail);
        boolean result = memberMerchantDetailWriteDao.insert(memberMerchantDetail);
        if (result){
            return memberMerchantDetail.getId();
        }
        return null;
    }

    /**
     * 更新
     * @param memberMerchantDetail 统计详情
     * @return 编号
     */
    @Override
    public boolean update(MemberMerchantDetail memberMerchantDetail) {
        memberMerchantDetail.setUpdateDate(DateUtils.today());
        return memberMerchantDetailWriteDao.update(memberMerchantDetail);
    }

    /**
     * 逻辑删除
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean deleteLogic(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)){
            return false;
        }
        return memberMerchantDetailWriteDao.deleteLogic(id);
    }

    /**
     * 物理删除
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean delete(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)){
            return false;
        }
        return memberMerchantDetailWriteDao.delete(id);
    }

    /**
     * 批量逻辑删除
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDeleteLogic(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return false;
        }
        return memberMerchantDetailWriteDao.batchDeleteLogic(ids);
    }

    /**
     * 批量物理删除
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDelete(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return false;
        }
        return memberMerchantDetailWriteDao.batchDelete(ids);
    }

    /**
     * 获取统计详情
     * @param id 主键
     * @return 统计详情
     */
    @Override
    public MemberMerchantDetail get(String id) {
        return memberMerchantDetailReadDao.get(id);
    }

    /**
     * 根据会员和时间获取
     * @param memberId 会员编号
     * @param date 时间
     * @return 统计详情
     */
    @Override
    public MemberMerchantDetail get(String memberId, Date date) {
        MemberMerchantDetail memberMerchantDetail = new MemberMerchantDetail();
        memberMerchantDetail.setMemberId(memberId);
        memberMerchantDetail.setCreateDate(date);
        List<MemberMerchantDetail> list = this.listPage(memberMerchantDetail, null);
        if (!CollectionUtils.isEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    /**
     * 获取或者设置
     * @param memberId 会员编号
     * @param date 时间
     * @return 统计详情
     */
    @Override
    public MemberMerchantDetail getSet(String memberId, Date date) {
        MemberMerchantDetail memberMerchantDetail = this.get(memberId, date);
        if (null != memberMerchantDetail){
            return memberMerchantDetail;
        }else {
            //总统计编号
            MemberMerchantCount memberMerchantCount = memberMerchantCountReadDao.findByMemberId(memberId);
            memberMerchantDetail = new MemberMerchantDetail(0, 0, 0, 0d, 0L, 0, 0);
            memberMerchantDetail.setMemberId(memberId);
            memberMerchantDetail.setCreateDate(date);
            if (null != memberMerchantCount){
                memberMerchantDetail.setCountId(memberMerchantCount.getId());
            }
            this.insert(memberMerchantDetail);
            return memberMerchantDetail;
        }
    }

    /**
     * 分页参数
     * @param memberMerchantDetail 统计详情
     * @param page 分页信息
     * @return 统计详情列表
     */
    @Override
    public List<MemberMerchantDetail> listPage(MemberMerchantDetail memberMerchantDetail, Page page) {
        return memberMerchantDetailReadDao.listPage(memberMerchantDetail, page);
    }

    /**
     * 统计详情所有列表
     * @param memberMerchantDetail 统计详情
     * @return 统计详情列表
     */
    @Override
    public List<MemberMerchantDetail> list(MemberMerchantDetail memberMerchantDetail) {
        return memberMerchantDetailReadDao.listPage(memberMerchantDetail, null);
    }

    /**
     * 统计
     * @param memberMerchantDetailQuery 统计参数
     * @return 统计列表
     */
    @Override
    public List<MemberMerchantDetailWithMember> sumList(MemberMerchantDetailQuery memberMerchantDetailQuery, DataParam dataParam, Page page) {
        List<MemberMerchantDetailWithMember> list;
        if (YesNoStatus.YES.getCode().equals(memberMerchantDetailQuery.getIsQueryChild())) {
            list = memberMerchantDetailReadDao.sumListWithChild(memberMerchantDetailQuery, dataParam, page);
        } else {
            list = memberMerchantDetailReadDao.sumList(memberMerchantDetailQuery, dataParam, page);
        }
        list = LangUtils.transform(list, new Function<MemberMerchantDetailWithMember, MemberMerchantDetailWithMember>() {
            @Override
            public MemberMerchantDetailWithMember apply(MemberMerchantDetailWithMember memberMerchantDetail) {
                memberMerchantDetail.setMoney(BigDecimalUtils.round(memberMerchantDetail.getMoney(), 2));
                return memberMerchantDetail;
            }
        });
        return list;
    }

    /**
     * 统计机构
     * @param memberMerchantDetailQuery 统计参数
     * @return 统计数据
     */
    @Override
    public MemberMerchantDetailWithMember sum(MemberMerchantDetailQuery memberMerchantDetailQuery) {
        List<MemberMerchantDetailWithMember> list = this.sumList(memberMerchantDetailQuery, null, null);
        if (!CollectionUtils.isEmpty(list)){
            return list.get(0);
        }
        return new MemberMerchantDetailWithMember(0, 0, 0, 0d, 0L, 0, 0);
    }

    @Override
    public List<MemberMerchantDetail> batchList(@NotNull Set<String> ids, MemberMerchantDetail memberMerchantDetail, Page page) {
        return null;
    }


    /**
     * 统计机构数据
     * @param memberMerchantDetailQuery 统计参数
     * @return 统计数据
     */
    @Override
    public MemberMerchantDetail allSum(MemberMerchantDetailQuery memberMerchantDetailQuery) {
        MemberMerchantDetail memberMerchantDetail = memberMerchantDetailReadDao.allSum(memberMerchantDetailQuery);
        if (null != memberMerchantDetail){
            return memberMerchantDetail;
        }
        return new MemberMerchantDetailWithMember(0, 0, 0, 0d, 0L, 0, 0);
    }

    /**
     * 根据条件查询
     * @param memberMerchantDetailQuery 查询参数
     * @return 统计列表
     */
    @Override
    public List<MemberMerchantDetail> queryList(MemberMerchantDetailQuery memberMerchantDetailQuery) {
        if (YesNoStatus.YES.getCode().equals(memberMerchantDetailQuery.getIsQueryChild())) {
            return memberMerchantDetailReadDao.queryListWithChild(memberMerchantDetailQuery);
        } else {
            return memberMerchantDetailReadDao.queryList(memberMerchantDetailQuery);
        }
    }

    /**
     * 根据条件查询所有
     * @param memberMerchantDetailQuery 查询参数
     * @return 统计列表
     */
    @Override
    public List<MemberMerchantDetail> allQueryList(MemberMerchantDetailQuery memberMerchantDetailQuery) {
        return memberMerchantDetailReadDao.allQueryList(memberMerchantDetailQuery);
    }

    /**
     * 清除多少天前的历史数据
     * @param num 多少天前
     * @return 删除结果(true/false)
     */
    @Override
    public boolean deleteHistory(Integer num) {
        return memberMerchantDetailWriteDao.deleteHistory(num);
    }

    @Override
    public List<MemberMerchantDetail> getAllViewNum(String createStart) {
        return memberMerchantDetailReadDao.getAllViewNum(createStart);
    }

    @Override
    public boolean batchUpdate(MemberMerchantDetail memberMerchantDetail) {
        return memberMerchantDetailWriteDao.batchUpdate(memberMerchantDetail);
    }
}
